const http = require('http');
const url = require('url');
const fs = require('fs');


const serverInstance = http.createServer();
const obj = {
    setData(req, res) {
    },
    getData(req, res) {
        res.end('Hello World!');
    }
}
serverInstance.on('request', (req, res) => {
    const { url, headers } = req;
    const ooo = dg(req);
    console.log(JSON.stringify(ooo));
    console.log(headers.host);

    // fs.writeFileSync('./data.txt', JSON.stringify(req));
    if ('/favicon.ico' === url) {
        res.end('nobody!!!!');
        return;
    }
    const fn = obj[url] || function (req, res) { res.end('NOT FOUND!') };
    fn(req, res);
});

var nnn = {
    name: 'Bob',
    age: 19,
    style: {
        color: 'red'
    }
};
// let ooo = {};

function dg(nnn) {
    let obj = {};
    for (let key in nnn) {
        const item = nnn[key];
        if (Object.prototype.toString.call(item) === '[object Object]') {
            obj[key] = dg(item);
        } else {
            obj[key] = item;
        }
    }
    return obj;
}

// const aa = new URL('/getData?name=Bob');
// console.log(aa[1]);
serverInstance.listen(3000);